//
//  GameModel.swift
//  Game
//
//  Created by woniu on 2025/8/27.
//

import Foundation
import UIKit
import HandyJSON

enum GameType: String {
    case color = "动画1-颜色"
    case expression = "动画2-翻牌"
    case expression2 = "动画3-滑动"
    case sentence = "句子-填空"
    case findWord = "查找单词"
    case ligature = "连线"
    case dragAndDropToFill = "单词-拖拽-图片"
    case sentenceAnswerSentence = "通过题目找句子答案"
    case weedOut = "消消乐"
    case poison = "毒药"
    case none = ""
    
    public static func cardTypes()->[GameType]{
        return [.color,.expression,.expression2,.ligature,.dragAndDropToFill]
    }
    
    public static func allTypes()->[GameType]{
        return [.color,.expression,.expression2,.sentence,.findWord,.ligature,.dragAndDropToFill,.sentenceAnswerSentence,.weedOut,.poison]
    }
}

class GameModel:NSObject,HandyJSON{
    required override init(){}
    //必填
    public var key = ""
    //必填
    public var name = ""
    //必填（枚举：GameType）
    public var type = ""
    //选填，其他数据1
    public var words:[WordItem] = []
    //选填，其他数据
    public var jsonData = ""
    
    public func getSubmitWords()->[WordItem]{
        var items:[WordItem] = []
        for word in words {
            let item = WordItem()
            item.word = word.word
            item.imgName = word.imgName
            items.append(item)
        }
        return items
    }
    
    //创建模型
    public class func createBy(type:GameType,name:String)->GameModel{
        let model = GameModel()
        model.key = DateUtil.currentDateTime(format: "yyyyMMddHHmmss")
        model.name = name
        model.type = type.rawValue
        return model
    }
    
    //默认
    public class func defaultModel()->GameModel{
        let model = GameModel()
        model.key = DateUtil.currentDateTime(format: "yyyyMMddHHmmss")
        
        for _ in 0...6 {
            let item = WordItem()
            item.word = ""
            item.imgName = ""
            model.words.append(item)
        }
        return model
    }
    
    //默认
    public class func defaultFindWordModel()->GameModel{
        let model = GameModel()
        model.key = DateUtil.currentDateTime(format: "yyyyMMddHHmmss")
        model.type = GameType.findWord.rawValue
        let config = FindeWordConfig()
        config.inputContentList = Array(repeating: "", count: 7)
        model.jsonData = config.toJSONString() ?? ""
        return model
    }
    
    //默认
    public class func defaultSentenceToSModel()->GameModel{
        let model = GameModel()
        model.key = DateUtil.currentDateTime(format: "yyyyMMddHHmmss")
        model.type = GameType.sentenceAnswerSentence.rawValue
        var inputItemList:[SentenceItem] = []
        for _ in 0...6{
            inputItemList.append(SentenceItem())
        }
        model.jsonData = inputItemList.toJSONString() ?? ""
        return model
    }
    
    //默认
    public class func defaultWeedOut()->GameModel{
        let model = GameModel()
        model.key = DateUtil.currentDateTime(format: "yyyyMMddHHmmss")
        model.type = GameType.weedOut.rawValue
        
        let config = FindeWordConfig()
        config.inputContentList = Array(repeating: "", count: 7)
        model.jsonData = config.toJSONString() ?? ""
        return model
    }
    
    //默认
    public class func defaultPosion()->GameModel{
        let model = GameModel()
        model.key = DateUtil.currentDateTime(format: "yyyyMMddHHmmss")
        model.type = GameType.poison.rawValue
        
        let config = FindeWordConfig()
        config.inputContentList = Array(repeating: "", count: 7)
        model.jsonData = config.toJSONString() ?? ""
        return model
    }
}

class WordItem:NSObject,HandyJSON{
    required override init(){}
    public var word = ""
    public var imgName = ""
    public var image:UIImage?
    public var didModifyImg = false
}
